﻿using System.Collections.Generic;
using System.Xml;
using InfoJetSoft.Service.Util;

namespace ServBus.ListForm2013.Utility
{
    public class FeatureChecker
    {
        public bool BrowserCompatible { get; private set; }
        public bool HasManagedCode { get; private set; }
        public bool EnableTopToolbar { get; private set; }
        public bool EnableBottomToolbar { get; private set; }
        public List<string> ToolbarActions { get; private set; }
        public List<string> ViewXsls { get; private set; }
        public List<string> ViewInfo { get; private set; }
        public string DefaultView { get; private set; }

        public FeatureChecker(XmlDocument xsfFile)
        {
            XmlNamespaceManager nsmgr = new NamespaceResolver(xsfFile);
            //XmlNamespaceManager nsmgr = new XmlNamespaceManager(xsfFile.NameTable);
            foreach (XmlAttribute att in xsfFile.DocumentElement.Attributes)
            {
                if (att.Prefix == "xmlns")
                {
                    if (!nsmgr.HasNamespace(att.LocalName))
                    {
                        nsmgr.AddNamespace(att.LocalName, att.Value);
                    }
                }
            }
            
            XmlNode compNode = xsfFile.SelectSingleNode("//xsf2:solutionDefinition[@runtimeCompatibility]", nsmgr);
            this.BrowserCompatible = (compNode != null && compNode.Attributes["runtimeCompatibility"].Value.ToLower().Contains("server"));

            XmlNode mcNode = xsfFile.SelectSingleNode("//xsf2:managedCode[@enabled]", nsmgr);
            this.HasManagedCode = (mcNode != null && mcNode.Attributes["enabled"].Value.ToLower() == "yes");

            XmlNode tbNode = xsfFile.SelectSingleNode("//xsf2:toolbar", nsmgr);
            this.EnableTopToolbar = (tbNode == null || tbNode.Attributes["enabledTop"] == null
                || tbNode.Attributes["enabledTop"].Value.ToLower() == "yes");
            this.EnableBottomToolbar = (tbNode == null || tbNode.Attributes["enabledBottom"] == null
                || tbNode.Attributes["enabledBottom"].Value.ToLower() == "yes");
            

            // Get View
            this.ViewXsls = new List<string>();
            XmlNodeList viewNodes = xsfFile.SelectNodes("//xsf:view/xsf:mainpane", nsmgr);
            foreach (XmlNode viewNode in viewNodes)
            {
                if (viewNode.Attributes["transform"] != null)
                    this.ViewXsls.Add(viewNode.Attributes["transform"].Value);
            }

            this.ViewInfo = new List<string>();
            XmlNodeList viewInfoNodes = xsfFile.SelectNodes("//xsf:view", nsmgr);
            int i = 0;
            foreach (XmlNode viewNode in viewInfoNodes)
            {
                if (viewNode.Attributes["name"] != null)
                    this.ViewInfo.Add(i.ToString() + "_" + viewNode.Attributes["name"].Value);
                i++;
            }

            XmlElement viewRootNode = (XmlElement) xsfFile.SelectSingleNode("//xsf:views", nsmgr);
            DefaultView = viewRootNode.GetAttribute("default");

        }
    }
}
